package com.rm2pt.generator.springboot.LabManage.controller;
import com.rm2pt.generator.springboot.LabManage.service.*;
import com.rm2pt.generator.springboot.LabManage.Message.*;
import java.util.Date;
import com.rm2pt.generator.springboot.LabManage.utils.PreconditionException;
import com.rm2pt.generator.springboot.LabManage.utils.StandardOPs;
import javax.servlet.http.HttpSession;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import org.springframework.web.bind.annotation.RequestMethod;

@RestController
@RequestMapping("/JoinTeamService")
public class JoinTeamServiceController {
	
	//@Autowired HttpSession hs;
	
	@Autowired 
	private JoinTeamService joinTeamService;
	
	@RequestMapping(value="/applyJoinTeam",method=RequestMethod.PUT)	
	public String applyJoinTeam(ApplyJoinTeamMessage applyJoinTeamMsg) {
		
		
		JSONObject object = new JSONObject();
		try {
			object.put("data", joinTeamService.applyJoinTeam(applyJoinTeamMsg.teamId,applyJoinTeamMsg.studentId));
			object.put("msg", "success");
			object.put("code", "200");
		} catch (Exception e) {
			if (e instanceof PreconditionException) {
				object.put("msg", "PreConditionException");
			}else {
				object.put("msg", "PostConditionException");
				e.printStackTrace();
			}
			object.put("code", "400");
		}
		String s=JSON.toJSONString(object);
		return s;
	}		
	// @RequestMapping(value="/applyJoinGroup",method=RequestMethod.PUT)	
	// public String applyJoinGroup(ApplyJoinGroupMessage applyJoinGroupMsg)
	
	
	@RequestMapping(value="/exitGroup",method=RequestMethod.DELETE)	
	public String exitGroup(ExitGroupMessage exitGroupMsg) {
		
		
		JSONObject object = new JSONObject();
		try {
			object.put("data", joinTeamService.exitGroup(exitGroupMsg.groupId,exitGroupMsg.studentId));
			object.put("msg", "success");
			object.put("code", "200");
		} catch (Exception e) {
			if (e instanceof PreconditionException) {
				object.put("msg", "PreConditionException");
			}else {
				object.put("msg", "PostConditionException");
				e.printStackTrace();
			}
			object.put("code", "400");
		}
		String s=JSON.toJSONString(object);
		return s;
	}
	@RequestMapping(value="/exitTeam",method=RequestMethod.DELETE)	
	public String exitTeam(ExitTeamMessage exitTeamMsg) {
		
		
		JSONObject object = new JSONObject();
		try {
			object.put("data", joinTeamService.exitTeam(exitTeamMsg.teamId,exitTeamMsg.studentId));
			object.put("msg", "success");
			object.put("code", "200");
		} catch (Exception e) {
			if (e instanceof PreconditionException) {
				object.put("msg", "PreConditionException");
			}else {
				object.put("msg", "PostConditionException");
				e.printStackTrace();
			}
			object.put("code", "400");
		}
		String s=JSON.toJSONString(object);
		return s;
	}
	
}
